package main

import (
	"fmt"
	"time"
)

// 最长子串
func main() {
	begin := time.Now()
	ans := solve2("abcbded")
	fmt.Println(ans)
	end := time.Now()
	fmt.Println(end.Sub(begin))
}

// common 常规解法
func common(s string) int {
	set := make(map[int32]struct{})
	ans := 0
	for i := range s {
		epoch := 0
		for _, j := range s[i:] {
			if _, ok := set[j]; ok {
				break
			}
			set[j] = struct{}{}
			epoch++
			if epoch > ans {
				ans = epoch
			}
		}
		set = make(map[int32]struct{})
	}
	return ans
}

// solve2 解法2，比解法 1 时间复杂度低，滑动区间
func solve2(s string) int {
	if len(s) == 0 {
		return 0
	}
	left := 0
	max := 0
	set := make(map[uint8]int)
	for i := range s {
		if o, ok := set[s[i]]; ok {
			left = o
		}
		set[s[i]] = i
		if i-left > max {
			max = i - left
		}
	}
	return max
}
